package org.happycomp.radiog;

import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IWorkbenchPartSite;

public class SyncExecTemplate {

	private IWorkbenchPartSite site;
	private Runnable runnable;
	private Display display;

	public SyncExecTemplate(IWorkbenchPartSite site, Runnable runnable) {
		super();
		this.site = site;
		this.runnable = runnable;
	}
	
	public SyncExecTemplate(Display disp, Runnable runnable) {
		super();
		this.display = disp;
		this.runnable = runnable;
	}
	
	
	public void execute() {
		if (this.display != null) executeFromWb();
		else executeFromPart();
	}
	
	private void executeFromPart() {
		if (site != null && site.getShell() != null && site.getShell().getDisplay() != null) {
			if (!site.getShell().getDisplay().isDisposed()) {
				site.getShell().getDisplay().syncExec(this.runnable);
			}
		}
	}
	
	private void executeFromWb() {
		if (display != null) {
			display.syncExec(this.runnable);
		}
	}
	
}
